﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework
 * Version 2.10
 * 
 * CNR Error Code Class
 *      Provides a basic definition for repsenting an error code value.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.Text;
using Adaptive.Foundation;

namespace Adaptive.Foundation.Network.Cnr
{
    /// <summary>
    /// Provides a basic definition for repsenting an error code value.
    /// </summary>
    public class CnrErrorCode<T> : CnrObjectBase, IErrorCode 
    {
        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// Error code value.
        /// </summary>
        private T _errorCodeValue;
        /// <summary>
        /// Description of the error code.
        /// </summary>
        private string _descriptionText;
        /// <summary>
        /// Any other related data.
        /// </summary>
        private object _relatedData;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="errorCodeValue">
        /// The error code value being represented.
        /// </param>
        public CnrErrorCode(T errorCodeValue)
            : base()
        {
            _errorCodeValue = errorCodeValue;
            _descriptionText = string.Empty;
            _relatedData = null;
        }
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="errorCodeValue">
        /// The error code value being represented.
        /// </param>
        /// <param name="description">
        /// Error text associated with the error code value.
        /// </param>
        public CnrErrorCode(T errorCodeValue, string description)
            : base()
        {
            _errorCodeValue = errorCodeValue;
            _descriptionText = description;
            _relatedData = null;
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (!Disposed)
            {
                _errorCodeValue = default(T);
            }
            _descriptionText = null;
            _relatedData = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets or sets the description of the value being represented.
        /// </summary>
        /// <value>
        /// Description text or an empty string.
        /// </value>
        public string Description
        {
            get { return _descriptionText; }
            set { _descriptionText = value; }
        }
        /// <summary>
        /// Gets or sets the value of the error code being represented.
        /// </summary>
        /// <value>
        /// An error code value.
        /// </value>
        public T ErrorCodeValue
        {
            get { return _errorCodeValue; }
            set { _errorCodeValue = value; }
        }
        /// <summary>
        /// Gets or sets a reference to any related data.
        /// </summary>
        /// <value>
        /// An object or <b>null</b>.
        /// </value>
        public object RelatedData
        {
            get { return _relatedData; }
            set { _relatedData = value; }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Clears the contents of the object.
        /// </summary>
        public override void Clear()
        {
            base.Clear();
            _descriptionText = string.Empty;
            _errorCodeValue = default(T);
            _relatedData = null;
        }
        /// <summary>
        /// Gets a string representation of the object.
        /// </summary>
        /// <returns>
        /// A string representing the error code and, optionally, the description text.
        /// </returns>
        public override string ToString()
        {
            StringBuilder builder = null;

            builder = new StringBuilder();
            builder.Append(_errorCodeValue.ToString());
            
            if (_descriptionText.Length > 0)
            {
                builder.Append(" ");
                builder.Append(_descriptionText);
            }

            return builder.ToString();
        }
        /// <summary>
        /// Renders the error code as text.
        /// </summary>
        /// <returns>
        /// The error code as a text string.
        /// </returns>
        public virtual string Render()
        {
            return ToString();
        }
        /// <summary>
        /// Renders the error code as text.
        /// </summary>
        /// <returns>
        /// The error code as a text string.
        /// </returns>
        public virtual string RenderErrorCodeValue()
        {
            return _errorCodeValue.ToString();
        }
        /// <summary>
        /// Renders the error code as text.
        /// </summary>
        /// <param name="renderer">
        /// The renderer used to render the text.
        /// </param>
        /// <returns>
        /// The error code as a text string.
        /// </returns>
        public virtual string Render(CnrDataRendererBase renderer)
        {
            string returnValue = string.Empty;

            try
            {
                returnValue = renderer.RenderErrorCode(this);
            }
            catch (Exception ex)
            {
                OnRenderingException(new ExceptionEventArgs(ex));
            }
            return returnValue;
        }
        /// <summary>
        /// Renders the error code as text.
        /// </summary>
        /// <param name="renderer">
        /// The renderer used to render the text.
        /// </param>
        /// <returns>
        /// The error code as a text string.
        /// </returns>
        public virtual string RenderErrorCodeValue(CnrDataRendererBase renderer)
        {
            string returnValue = string.Empty;

            try
            {
                returnValue = renderer.RenderErrorCodeValue(_errorCodeValue.ToString());
            }
            catch (Exception ex)
            {
                OnRenderingException(new ExceptionEventArgs(ex));
            }
            return returnValue;
        }
        #endregion
    }
}